﻿
Imports System.Net

Public Class IPEndPoint2
    Inherits IPEndPoint
    Implements ICloneable, IComparable, IComparable(Of IPEndPoint), IComparable(Of IPEndpoint2)

    Public Sub New(ByVal address As Int64, ByVal port As Int32)
        MyBase.New(address, port)
    End Sub

    Public Sub New(ByVal address As IPAddress, ByVal port As Int32)
        MyBase.New(address, port)
    End Sub

    Public Sub New(ByVal this As IPEndPoint)
        MyBase.New(this.Address, this.Port)
    End Sub

    Public Overloads Shared Function create(ByVal address As String, ByVal port As Int32) As IPEndPoint
        Dim rtn As IPEndPoint = Nothing
        If parse(address, port, rtn) Then
            Return rtn
        Else
            Return Nothing
        End If
    End Function

    Public Overloads Shared Function create(ByVal address_port As String) As IPEndPoint
        Dim rtn As IPEndPoint = Nothing
        If parse(address_port, rtn) Then
            Return rtn
        Else
            Return Nothing
        End If
    End Function

    Public Shared Function parse(ByVal address As String, ByVal port As Int32, ByRef rtn As IPEndPoint) As Boolean
        Dim tmp As IPAddress = Nothing
        If IPAddress2.parse(address, tmp) Then
            rtn = New IPEndPoint2(tmp, port)
            Return True
        Else
            Return False
        End If
    End Function

    Public Shared Function parse(ByVal address_port As String, ByRef rtn As IPEndPoint) As Boolean
        Dim address As String = Nothing
        Dim port As Int32 = 0
        If parseHostPort(address_port, address, port) Then
            Return parse(address, port, rtn)
        Else
            Return False
        End If
    End Function

    Public Function Clone() As Object Implements ICloneable.Clone
        Return New IPEndpoint2(Address, Port)
    End Function

    Public Function CompareTo(ByVal obj As Object) As Int32 Implements IComparable.CompareTo
        Return CompareTo(convertor.cast(Of IPEndPoint)(obj, False))
    End Function

    Public Function CompareTo(ByVal other As IPEndPoint) As Int32 Implements IComparable(Of IPEndPoint).CompareTo
        If other Is Nothing Then
            Return 1
        Else
            Dim cmp As Int32 = 0
            cmp = IPAddress2.compare(Address, other.Address)
            If cmp = 0 Then
                Return compare(Port, other.Port)
            Else
                Return cmp
            End If
        End If
    End Function

    Public Function CompareTo(ByVal other As IPEndpoint2) As Int32 Implements IComparable(Of IPEndpoint2).CompareTo
        Return CompareTo(convertor.cast(Of IPEndPoint)(other, False))
    End Function
End Class
